package main

import (
	"bufio"
	"fmt"
	"log"
	"math/rand"
	"os"
	"strconv"
	"strings"
	"time"
)

func main() {
	nowTime := time.Now().Unix()
	rand.Seed(nowTime)
	target := rand.Intn(100) + 1
	reader := bufio.NewReader(os.Stdin)

	success := false
	//循环
	for guessed := 0; guessed < 10; guessed++ {
		fmt.Println("you have", 10-guessed, "guesses left")
		//获取用户的输入
		fmt.Print("Please input a number: ")
		input, err := reader.ReadString('\n')
		// 异常处理
		if err != nil {
			log.Fatal(err)
		}
		//删除换行符
		input = strings.TrimSpace(input)
		//转化为整数
		guess, err := strconv.Atoi(input)
		if err != nil {
			log.Fatal(err)
		}

		//整数
		if guess > target {
			fmt.Println("your guess is high!")
			continue
		} else if guess < target {
			fmt.Println("your number is low!")
			continue
		} else{
			success = true
			fmt.Println("you are right!")
			break
		}
	}

	if success == false {
		fmt.Println("Sorry, your guess is error, the number is ", target)

	}

}
